class StudentController {

    def timeUtilService
    def classesService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def authenticateService
    def createBlogService
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [studentInstanceList: Student.list(params), studentInstanceTotal: Student.count()]
    }

    def create = {
        def studentInstance = new Student()
        studentInstance.properties = params
        return [studentInstance: studentInstance,aClasses:Classes.get(params.id),id:params.id]
    }

    def save = {
        // 由于前台用了jquery的日期插件，这里需要转换日期的格式
        Date birthday = timeUtilService.stringToDate(params.birthday)
        log.info(birthday)
        params.birthday = birthday

        def studentInstance = new Student(params)
        if(params.state == null){
            studentInstance.source = ''
            //studentInstance.enabled = false;
        }else{
            studentInstance.source = params.source
        }
        if(session.schoolId){
            studentInstance.school = School.get(session.schoolId)
        }
        
        if (studentInstance.save(flush: true)) {
            if(createBlogService.sendCreateBlog(studentInstance,grailsApplication.config.grails.default.blog.create.uri)){
                studentInstance.hasBlog = true
                studentInstance.save(flush:true)
                flash.message = "创建学生${studentInstance.name}成功，同时建立了学生博客！"
            }else{
                flash.message = "创建学生${studentInstance.name}成功，创建学生博客失败，请在学生列表中再次进行创建，如果依然有错误，请联系管理员！"
            }
            return redirect(action: "classesStudents",controller:"studentMgr",id:params.classesID)
        }else {
            render(view: "create", model: [studentInstance: studentInstance,aClasses:Classes.get(params.classesID)])
        }
    }

    def show = {
        def studentInstance = Student.get(params.id)
        if (!studentInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])}"
            return redirect(action: "classesStudents",controller:"studentMgr",id:params.classesID)
        }
        else {
            [studentInstance: studentInstance,classesID:params.classesID]
        }
    }

    def edit = {
        def studentInstance = Student.get(params.id)
        if (!studentInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])}"
            redirect(action: "classesStudents",controller:"studentMgr",id:params.classesID)
        }
        else {
            return [studentInstance: studentInstance,classesID:params.classesID]
        }
    }

    def update = {
        def studentInstance = Student.get(params.id)
        if (studentInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (studentInstance.version > version) {                    
                    studentInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'student.label', default: 'Student')] as Object[], "Another user has updated this Student while you were editing")
                    render(view: "edit", model: [studentInstance: studentInstance,classesID:params.classesID])
                    return
                }
            }
             // 由于前台用了jquery的日期插件，这里需要转换日期的格式
            Date birthday = timeUtilService.stringToDate(params.birthday)
            log.info(birthday)
            params.birthday = birthday
            studentInstance.properties = params
            if(params.state == null){
                studentInstance.source = ''
                studentInstance.enabled = false;
            }else{
                studentInstance.source = params.source
            }
            if (!studentInstance.hasErrors() && studentInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: ['信息', studentInstance.name])}"
                return redirect(action: "classesStudents",controller:"studentMgr",id:params.classesID)
            }
            else {
                render(view: "edit", model: [studentInstance: studentInstance,classesID:params.classesID])
            }
        }
        else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])}"
                return redirect(action: "classesStudents",controller:"studentMgr",id:params.classesID)
            
        }
    }

    def delete = {
        def studentInstance = Student.get(params.id)
        if (studentInstance) {
            try {
                studentInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'student.label', default: 'Student'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'student.label', default: 'Student'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])}"
            redirect(action: "list")
        }
    }
}
